package com.lq.ecoldchain.ui.main.orderlist.order.detail

import android.arch.lifecycle.MutableLiveData
import com.lq.ecoldchain.base.viewmodel.BaseViewModel
import com.lq.ecoldchain.common.PhotoAddRecyclerAdapter
import com.lq.ecoldchain.internet.BaseObserver
import com.lq.ecoldchain.util.PhotoShowUtils
import com.lq.ecoldchain.util.toast

/*
*2019/1/17 0017  14:38
*function by lq
*/
class HandleOrderViewModel : BaseViewModel() {
    val list by lazy { ArrayList<String>() }
    val adapter by lazy { PhotoAddRecyclerAdapter(list) }
    val explain by lazy { MutableLiveData<String>() }
    var id: Int = 0

    init {
        list.add("")
        adapter.setOnItemChildClickListener { _, view, position ->
            if (list[position].isEmpty())
                PhotoShowUtils.choosePhoto(view)
        }
    }

    fun add(url: String) {
        list.remove("")
        list.add(url)
        list.add("")
        adapter.notifyDataSetChanged()
    }

    fun cargoHanding() {
        if (list.size > 1 && id != 0 && !explain.value.isNullOrEmpty()) {
            val imgs = StringBuffer()
            for (i in list) {
                if (i.isNotEmpty()) {
                    imgs.append("$i,")
                }
            }
            val data = imgs.toString().substring(0, imgs.toString().length - 1)
            BaseNetCore.ApiServiceImpl.cargoHanding(id, explain.value!!, data).compose(RxTransformer.switchSchedulers()).subscribe(object : BaseObserver<String>() {
                override fun onSuccess(t: String?) {}

                override fun onFail(code: String) {}
            })
        } else {
            toast { "请填写完整信息" }
        }

    }
}